//
//  DownloadViewController.m
//  UADE-Mobile
//
//  Created by Marcos Damian Pianelli on 10/4/11.
//  Copyright (c) 2011 DG. All rights reserved.
//

#import "DownloadViewController.h"
#import "TTCustomDataSource.h"
#import "TTTableFileItem.h"


@implementation DownloadViewController
@synthesize userInfo, aulaID;




-(void)createTableView{
    
    // Do any additional setup after loading the view from its nib.
    
    NSMutableArray * sections = [NSMutableArray array];
    NSMutableArray * items = [NSMutableArray array];
    
    
    for( NSDictionary * eachFolder in self.userInfo){
        
        [sections addObject:[eachFolder objectForKey:@"Nombre"]];
        
        NSMutableArray * section = [NSMutableArray array];
        [items addObject:section];
        
        
        for(NSDictionary * eachFile in [eachFolder objectForKey:@"Archivos"]){
            
            if( ![[eachFile objectForKey:@"Extension"] isEqualToString:@".rar"] )
                [section addObject:[TTTableFileItem itemWithDictionary:eachFile URL:nil]];
            
        }
        
    }
    
    
    // This demonstrates how to create a table with standard table "fields".  Many of these
    // fields with URLs that will be visited when the row is selected
    self.dataSource = [TTCustomDataSource dataSourceWithItems:items sections:sections]; 
}



- (id)initWithNavigatorURL:(NSURL*)URL query:(id)query {

    self = [super initWithNibName:@"DownloadViewController" bundle:nil];
    
    if (self) {
    
        self.title = NSLocalizedString(@"Download", @"");
        
        self.userInfo = query;
        NSMutableArray * sections = [NSMutableArray array];
        NSMutableArray * items = [NSMutableArray array];
        
        
        [sections addObject:NSLocalizedString(@"Group Files", @"")];
            
        NSMutableArray * section = [NSMutableArray array];
        [items addObject:section];
            
            
        for(NSDictionary * eachFile in self.userInfo){
            if( ![[eachFile objectForKey:@"Extension"] isEqualToString:@".rar"] )
                [section addObject:[TTTableFileItem itemWithDictionary:eachFile URL:nil]];
                
        }
            
        // This demonstrates how to create a table with standard table "fields".  Many of these
        // fields with URLs that will be visited when the row is selected
        
        self.dataSource = [TTCustomDataSource dataSourceWithItems:items sections:sections];        
    }
    
    return self;
    
}




-(id)initWithID:(NSString*)IDAula{
    
    
    self = [super initWithNibName:@"DownloadViewController" bundle:nil];
    
    if (self) {
        
        self.title = NSLocalizedString(@"Download", @"");
        
        self.aulaID = IDAula;
       /* 
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(closeAction:)];
        */
        
        [[StudentModel SharedInstance] fetchMoreInfoWithDelegate:self aulaID:self.aulaID];

    }
    
    return self;    

    
}




-(IBAction)closeAction:(id)sender{
    [self dismissModalViewControllerAnimated:TRUE];
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    self.variableHeightRows = TRUE;
    self.showTableShadows = TRUE;    
    
}

- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath{

    
    [[TTNavigator navigator] openURLAction:
     [[[TTURLAction actionWithURLPath:@"tt://downloadDetail"] applyQuery:[(TTTableFileItem*)object fileInfo]] applyAnimated:TRUE]];
    
    
}


-(void)didGetMoreStudentInfo:(id)model withError:(NSError*)error{
    
    
    self.userInfo = [(NSDictionary*)model objectForKey:@"Carpetas"];
    
    [self createTableView];
   

}




- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
